var gspz = {
    setBtnStatus: function () {
        var row = $('#grid_gspz').datagrid('getSelected');
        if (row && row.id && row.sfqy == '否') {
            $("#btn_gspz_mod").show();
            $("#btn_gspz_start").show();
        } else if (row && row.id && row.sfqy == '是') {
            $("#btn_gspz_mod").hide();
            $("#btn_gspz_start").show();
        } else {
            $("#btn_gspz_mod").hide();
            $("#btn_gspz_start").hide();
        }
    }
};

$(function () {
    //表格初始加载
    $('#grid_gspz').datagrid({
        url: $.webPath() + "/base/gspz/gspzList",
        toolbar: "#toolbar_gspz",
        fitColumns: true,
        pageNumber: 1,
        singleSelect: true,
        pagination: true,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {field: 'mc', title: '公式名称', width: 100, formatter: gspzCell},
                {field: 'szr_xm', title: '设置人', width: 100},
                {field: 'jzrq', title: '截止日期', width: 100},
//                {field: 'zt', title: '状态', width: 100,
//                    formatter: function (value) {
//                        if (value == 'ybc')
//                            return '已保存';
//                        else
//                            return '已审批';
//                    }
//                },
                {field: 'sfqy', title: '启用', width: 100,
                    formatter: function (value) {
                        if (value == '是')
                            return '是';
                        else
                            return '否';
                    }
                }
            ]
        ],
        onLoadSuccess: function () {
            $(this).datagrid('selectRow', 0);
            gspz.setBtnStatus();
        },
        onClickRow: function () {
            gspz.setBtnStatus();
        },
        onSelect: function (rowIndex, rowData) {
            if (rowData.sfqy == null || rowData.sfqy == '否') {
                $("#btn_gspz_start").text('启用');
                $("#btn_gspz_start").removeClass('stop').addClass('start');
                $('#btn_gspz_start').unbind('click');
                $('#btn_gspz_start').bind('click', function () {
                    changegspzState($.webPath() + '/base/gspz/start_gspz', '确认启用此公式配置？');
                });
            } else {
                $("#btn_gspz_start").text('停用');
                $("#btn_gspz_start").removeClass('start').addClass('stop');
                $('#btn_gspz_start').unbind('click');
                $('#btn_gspz_start').bind('click', function () {
                    changegspzState($.webPath() + '/base/gspz/stop_gspz', '确认停用此公式配置？');
                });
            }
            gspz.setBtnStatus();
        },
        rowStyler: function (index, row) {
            if (row.sfqy == '否') {
                return 'background-color:rgba(255, 130, 87, 0.64);';
            }
        }
    });

    //**********按钮触发事件**********
    $("#btn_gspz_add").click(function () {
        gspz_add("新增公式", "add_gspz");
    });

    //修改按钮
    $("#btn_gspz_mod").click(function () {
        gspz_modify('修改公式', 'update_gspz');
    });
    //启用-停用按钮
    $("#btn_gspz_start").click(function () {
        changegspzState();
    });
    //查询按钮
    $("#btn_gspz_search").click(function () {
        gspz_search();
    });
});
//新增
function gspz_add(title, action) {
    var href = $.webPath() + "/base/gspz/gspz_content";
    var actionUrl = $.webPath() + "/base/gspz/" + action;
    gspz_showDialog(title, href, actionUrl);
}
//修改
function gspz_modify(title, action) {
    var record = $("#grid_gspz").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/base/gspz/gspz_content?gspzId=" + record.id;
    var actionUrl = $.webPath() + "/base/gspz/" + action;
    gspz_showDialog(title, href, actionUrl);
}

//查询短信配置
function gspz_search() {
    $('#grid_gspz').datagrid('load',
        {
            mc: $('#gspz_mc').val()
        }
    );
}

//新增或修改短信配置弹出窗口
function gspz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 820,
        height: 400,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#gspzForm').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var validate = $(this).form('validate');
                            if (!validate) {
                                $('#gspzForm').tabs('select', "公式信息");
                                $.messager.alert("提示", "请填写必填项!", "waring");
                                $(win).mask('hide');
                                return false;
                            }
                            $('#gspz_adsq_table').edatagrid("saveRow");
                            $('#gspz_atsq_table').edatagrid("saveRow");
                            var data = $('#gspz_adsq_table').edatagrid("getData");
                            var fydata = $('#gspz_atsq_table').edatagrid("getData");
                            if (fydata.total == 0 && data.total == 0) {
                                $('#gspzForm').tabs('select', "按吨收取");
                                $.messager.alert("提示", "按吨收取和按天收取规则请选择一种或两种必填!", "waring");
                                $(win).mask('hide');
                                return false;
                            }
                            var arrayPrefix = 'gspz.adsqList';
                            var fyPrefix = 'gspz.atsqList';

                            var paramString = "{";
                            if (data.total != 0) {
                                for (var i = 0; i < data.rows.length; i++) {
                                    {
                                        var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                        paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                    }
                                }
                            }
                            if (fydata.total != 0) {
                                for (var i = 0; i < fydata.rows.length; i++) {
                                    {
                                        var objectString = $.toJSONStringSubPrefix(fydata.rows[i], fyPrefix + "[" + i + "].",
                                            [
                                                "id",
                                                "fklx.id",
                                                "startDate",
                                                "endDate",
                                                "llgs"
                                            ]);
                                        paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                    }
                                }
                            }

                            paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return validate;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_gspz').datagrid('reload');
                                $('#layout_gspz').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//改变状态，启用停用
function changegspzState(url, message) {
    var gspzId = $("#grid_gspz").datagrid("getSelected").id;
    if (gspzId) {
        $.messager.confirm("确认", message, function (r) {
            if (r) {
                $.ajax({
                    url: url + "?gspzId=" + gspzId,
                    type: "json",
                    success: function (data) {
                        if (data.statusCode == '200') {
                            $('#grid_gspz').datagrid('reload');
                            $.messager.show({
                                title: '提示框',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert('提示', data.message, 'info');
                        }
                    }
                });
            }
        })
    }
}

